package com.example.demo.arithmetic;

/**
 * Description: 数组测试-最大子序列和<br/>
 *
 * @author liulonglong
 * @taskId <br/>
 * @Date 2020/3/13 14:23
 * @Version 1.0
 */
public class ArrayTest3 {

    public static int maxSubSum(int[] arr) {
        int maxSum = 0, thisSum = 0;
        for (int j = 0; j < arr.length; j++) {
            thisSum += arr[j];
            if (thisSum > maxSum) {
                maxSum = thisSum;
            } else if (thisSum < 0) {
                thisSum = 0;
            }
        }
        return maxSum;
    }

    public static void main(String[] args) {
        System.out.println(maxSubSum(new int[] { 1, 2, 3, -1, -2, -3, 1 }));
    }
}
